package com.xu.quartz;

import com.xu.mapper.CollegeMapper;
import com.xu.mapper.CourseMapper;
import com.xu.pojo.College;
import com.xu.pojo.Course;
import com.xu.pojo.CourseDto;
import com.xu.utils.RedisUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.HashMap;
import java.util.List;

public class PreSelectJob implements Job {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CollegeMapper collegeMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("准备选课");
        //将课程信息和课程存量备份到redis
        List<Course> courses = courseMapper.queryCourse();
        HashMap<String, Object> stockMap = new HashMap<>();
        HashMap<String, Object> coursesMap = new HashMap<>();

        for (Course course:courses){
            coursesMap.put(Integer.toString(course.getCourseId()),new CourseDto(course));
            stockMap.put(Integer.toString(course.getCourseId()), course.getStock());
        }
        redisUtil.hmset("course_info",coursesMap);
        redisUtil.hmset("course_stock",stockMap);
        System.out.println("准备选课完成");
    }
}
